<template>
  <div class="login-wrapper">
    <div class="background-image" :style="{ backgroundImage: 'url(' + bgImage + ')' }"></div>

      <div class="header-logo">
        <img src="@/assets/logo.png" alt="Abcell Logo" class="logo-img">
        <span class="logo-title">Abcell 生物科技</span>
      </div>

    <div class="login-content">
      <div class="login-container">
        <div v-if="showNotRegistered" class="alert-message">
          该手机号未注册，请先<a href="/register">注册</a>
        </div>
        <h2 class="login-title">系统登录</h2>
        <form @submit.prevent="handleSubmit" method="post" class="login-form">
          <div class="form-group">
            <input
              type="text"
              v-model="form.username"
              placeholder="用户名"
              class="form-input"
              required
            >
          </div>

          <div class="form-group">
            <input
              type="password"
              v-model="form.password"
              placeholder="密码"
              class="form-input"
              required
            >
          </div>

          <div class="role-group">
            <label
              v-for="role in roles"
              :key="role.value"
              class="role-label"
            >
              <input
                type="radio"
                v-model="form.role"
                :value="role.value"
                class="role-radio"
              >
              <span class="role-text">{{ role.label }}</span>
            </label>
          </div>

          <button type="submit" class="login-btn">登 录</button>

          <div class="footer-links">
            <span @click="handleRegister">注册账号</span>
            <span class="divider">|</span>
            <span @click="handleForget">忘记密码</span>
          </div>
        </form>
      </div>
    </div>

    <div class="login-footer">
      <p>©2025 Abcell 生物科技 | 版本 v2.1.0</p>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'LoginPage',
  data () {
    return {
      bgImage: require('@/assets/bg.png'),
      form: {
        username: '',
        password: '',
        role: '1'
      },
      showNotRegistered: false,
      roles: [
        { value: 1, label: '普通用户' },
        { value: 2, label: '实验室管理员' },
        { value: 3, label: '系统管理员' }
      ]
    }
  },
  methods: {
    async handleSubmit () {
      try {
        const response = await axios.post('http://localhost:8000/api/auth/login/',
          {
            username: this.form.username,
            password: this.form.password,
            role: parseInt(this.form.role)
          }, {
            headers: {
              'Content-Type': 'application/json'
            },
            transformRequest: [function (data) {
              return JSON.stringify(data)
            }]
          })

        // 存储token
        localStorage.setItem('access_token', response.data.access)
        localStorage.setItem('refresh_token', response.data.refresh)
        localStorage.setItem('user_role', response.data.role)
        localStorage.setItem('user_info', JSON.stringify(response.data.user))

        // 显示成功消息
        this.$message.success('登录成功')

        // 跳转到首页
        await this.$router.push('/Userhome')
      } catch (error) {
        console.error('登录失败:', error)

        // 错误处理
        if (error.response) {
          switch (error.response.status) {
            case 401:
              // this.$message.error('用户名或密码错误')
              alert('该用户名不存在，请注册或重新登录！')
              break
            case 400:
              this.$message.error('请求参数有误')
              break
            default:
              this.$message.error(`登录失败 (${error.response.status})`)
          }
        } else {
          this.$message.error('网络错误，请检查连接')
        }
      }
    },
    handleRegister () {
      this.$router.push('/register')
    },
    handleForget () {
      this.$router.push('/ForgotPassword')
    }
  }
}
</script>

<style scoped>
.login-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;
}

.background-image {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 100%;

}

.header-logo {
  position: absolute;
  top: 20px;
  right: 20px;
  display: flex;
  align-items: center;
  z-index: 1;
}

.logo-img {
  height: 32px;
  margin-right: 10px;
}

.logo-title {
  color: white;
  font-size: 18px;
  font-weight: bold;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.login-content {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: 100vh;
  padding-right: 25%;
}
.alert-message {
  color: #f56c6c;
  padding: 10px;
  margin-bottom: 15px;
  background-color: #fef0f0;
  border-radius: 4px;
}
.alert-message a {
  color: #409eff;
  text-decoration: none;
}
.login-container {
  width: 320px;
  padding: 30px;
  background-color: rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(8px);
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.login-title {
  text-align: center;
  margin-bottom: 25px;
  color: #fff;
  font-size: 18px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.form-group {
  margin-bottom: 18px;
}

.form-input {
  width: 100%;
  padding: 10px 15px;
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 6px;
  font-size: 13px;
  box-sizing: border-box;
  background-color: rgba(255, 255, 255, 0.2);
  color: #fff;
}

.form-input::placeholder {
  color: rgba(255, 255, 255, 0.7);
}

.form-input:focus {
  outline: none;
  border-color: rgba(255, 255, 255, 0.8);
  background-color: rgba(255, 255, 255, 0.3);
}

.role-group {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  gap: 8px;
}

.role-label {
  display: flex;
  align-items: center;
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s;
  background-color: rgba(255, 255, 255, 0.1);
}

.role-radio {
  appearance: none;
  width: 14px;
  height: 14px;
  border: 1px solid rgba(255, 255, 255, 0.6);
  border-radius: 50%;
  margin-right: 6px;
  transition: all 0.2s;
  position: relative;
}

.role-radio:checked {
  background-color: #409EFF;
  border-color: #409EFF;
}

.role-radio:checked::after {
  content: '';
  position: absolute;
  width: 6px;
  height: 6px;
  background-color: white;
  border-radius: 50%;
  top: 3px;
  left: 3px;
}

.role-text {
  font-size: 10px;
  color: #fff;
}

.login-btn {
  display: block;
  width: 100%;
  margin: 0 auto 15px;
  padding: 10px;
  background: linear-gradient(135deg, #409EFF, #66b1ff);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 2px 10px rgba(64, 158, 255, 0.3);
  letter-spacing: 1px;
}

.login-btn:hover {
  background: linear-gradient(135deg, #66b1ff, #409EFF);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
}

.footer-links {
  text-align: center;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
}

.footer-links span {
  cursor: pointer;
  transition: color 0.3s;
}

.footer-links span:hover {
  color: #fff;
  text-decoration: underline;
}

.divider {
  margin: 0 10px;
  color: rgba(255, 255, 255, 0.5);
}

.login-footer {
  position: absolute;
  bottom: 20px;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.7);
  font-size: 12px;
}
</style>
